package org.authsum.stitches.wicket.contentdef.table;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.authsum.stitches.external.ContentDefHolder;


import org.authsum.stitches.wicket.common.PageableTable;
import org.authsum.stitches.wicket.contentdef.ContentDefSelectionListener;



public class ContentDefTable  extends PageableTable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ContentDefTable(String id,List docs,ContentDefSelectionListener contentDefSelectionListener) {
		super(id,docs);
		this.contentDefSelectionListener = contentDefSelectionListener;
		this.postInit();

	}
	
	
	
	protected PageableListView getPageableListView() {
		PageableListView  plv = new PageableListView("rows", docs, pageCount) {
				protected void populateItem(ListItem item) {
					ContentDefHolder cdh = (ContentDefHolder) item.getModelObject();
					// item.add(new Label("name", dto.getName()));

					Panel pnl = getActionPanel(item);
					item.add(pnl);

					Label lbl = new Label("description", cdh.getName());
					lbl.setEscapeModelStrings(false);
					item.add(lbl);

				}
			};
			return plv;
			
	}
	
	


	


	@Override
	public Panel getActionPanel(ListItem item) {
		return new ContentDefActionsPanel(item, "name");
	}


	private class ContentDefActionsPanel extends Panel {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public ContentDefActionsPanel(ListItem listItem, String id) {
			super(id);
			AjaxLink link = (new AjaxLink("selectAjaxLink", listItem.getModel()) {
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					ContentDefHolder cdh = (ContentDefHolder) getModel().getObject();

					contentDefSelectionListener.contentDefSelected(target,cdh);
					//modalWindow.close(target);

				}
			});
			add(link);
			ContentDefHolder dto = (ContentDefHolder) listItem.getModel().getObject();
			String cdName = dto.getName();
			link.add(new Label("nameLabel", cdName));

		}

	}

	
	private ContentDefSelectionListener contentDefSelectionListener = null;
	
}
